class PlayerMelee extends GameCharacter
{
	private var _cover:boolean;
	private var covermeSound:AudioClip;
	
	public function PlayerMelee()
	{
		super();
		
		characterName = "Melee";
		
		//Attack
		attackRadius = 2;
		attackDamage = 30;
		actualDamage = 30;
		_attackPercent = 50.0;
		_cover = false;
		
		//Sight
		sightRadius = 8;
		moveRadius = 6;
		
		//Health Status
		maxHealth = 200;
		myHealth = maxHealth;	
		
		//For diplaying mouse over
		healthPercent = 100.0f;
		healthBarLength = Screen.width/9;
		style = new GUIStyle();
	    texture = new Texture2D(1, 1);
		
		//Player type
		_playerType = PlayerType.PLAYER_MELEE;
		
		buttonSkillTexture = Resources.Load("Buttons/cover_buttonNormal");
	 	buttonSkillHoverTexture = Resources.Load("Buttons/cover_buttonHover");
	 	buttonSkillPressedTexture = Resources.Load("Buttons/cover_buttonPressed");			
	 		 	
	 	skillstyle = new GUIStyle();
		skillstyle.normal.background = buttonSkillTexture;
		skillstyle.hover.background = buttonSkillHoverTexture;
		skillstyle.active.background = buttonSkillPressedTexture;	
		
		selectedSound = Resources.Load("meleeSound");
		covermeSound = Resources.Load("covermeSound");		
	}
	
	public function attack(target:EnemyCharacter, projectileType:int)
	{
		super.attack(target, projectileType);
		
		//calcualte hit chance
		var hitChance = calculateHitChance(target) + attackChanceModifier;
		
		//fire bullet
		var bullet = Resources.Load("Projectile_melee");
		bullet = Instantiate(bullet);
		var projectile = bullet.GetComponent(Projectile);
		
		var chance = Random.Range(0,100);
		
		//random attack chance
		if (chance < hitChance){
			//hit
			projectile.setup(this, target, true, projectileType);
			attackChanceModifier = 0;
		}else{
			//miss
			projectile.setup(this, target, false, projectileType);
			attackChanceModifier += Mathf.Floor((chance - hitChance)/5.0);
		}	
		
		Destroy(bullet,5);
		if (projectileType == Projectile.NORMAL)
			endTurn();
	}
	
	//OVERRIDE THIS FUNCTION IN EVERY INHERITED CLASS
	public function calculateHitChance(target:EnemyCharacter)
	{
		//currently calculating based on an average atk
		//calculate distance of enemy
		var distance = Vector3.Distance(target.getSurfaceCoord(), this.getSurfaceCoord());
		//based on the maximum range of weapon, calculate the hit chance based on the optimized range
		//close - mid range weapon
		var optimizedRangeMax = 5;
		var hitChance = 100;
		//too far
		if (distance > optimizedRangeMax)
			hitChance -= (distance - optimizedRangeMax) * 20;
		//if in cover
		if (target.isInCover())
		{
			if (!target.isFlanked(this))
				hitChance -= Obstacle.HIT_CHANCE_REDUCTION;
		}
		
		//check height advantage
		var heightDifference = target.getSurfaceCoord().y - this.getSurfaceCoord().y;
		if (heightDifference > 0)
			hitChance -= heightDifference * 10;
		else if (heightDifference < 0)
			hitChance += heightDifference * -10;
		
		//minimum 10%
		if (hitChance < 10)
			hitChance = 10;
		else if (hitChance > 100)
			hitChance = 100;
		return hitChance;
	}
	
	public function calculateActualDamage(target:EnemyCharacter)
	{
		//if in cover
		if (target.isInCover())
		{
			//check whether flanked
			if (target.isFlanked(this))
			{
				return attackDamage *2; 
			}
			else
			{
				return attackDamage;
			}
		}
		//not in cover
		else
		{
			return attackDamage *2;
		}
	}
	
	public function skill() // Charge and attack with high damage
	{
		// If skill is not on cooldown
		if(!_isCooldown){
			setAnimationState(SKILL_ANI);
			startCooldown();
			
			audio.PlayClipAtPoint(covermeSound,Vector3(0,0,0));				
			_cover = true;
			
			_tileReference.manualSetObstacle(_cover);
			
			setAnimationState(SKILL_ANI);
			
			Debug.Log("Player in Cover");
			
			endTurn();
		}
	}
	
	//@Override resetTurn
	public function resetTurn(){
		super.resetTurn();		
		
		if(_cover){
			_cover = false;
			Debug.Log("Player not in Cover");
			_tileReference.setIsCover(_cover);
			_tileReference.manualSetObstacle(_cover);
			setAnimationState(WALK_ANI);
			setAnimationState(STOP_ANI);
		}
	}
	
	public function returnToCoverAnimation()
	{
		if (_tileReference.isCover() && _currentAniState != SKILL_ANI)
			setAnimationState(COVER_IN_ANI);
	}
}